<?php

namespace plugin\agent\app\model;

use app\model\user\User;
use think\Model;

/**
 * t_agent_order 分销订单
 * @property integer $id id(主键)
 * @property integer $order_id 订单id
 * @property string $order_no 订单号
 * @property integer $buy_user_id 购买人id
 * @property integer $agent_user_id 返佣人id
 * @property string $order_amount 订单金额
 * @property string $pay_amount 支付金额
 * @property string $agent_percent 返佣比例
 * @property string $spread_amount 返佣金额
 * @property integer $status 1:待结算  2:冻结中 3:已退款 4:已结算
 * @property string $create_time 创建时间
 * @property string $update_time 更新时间
 */
class AgentOrder extends Model
{
    /**
     * The connection name for the model.
     *
     * @var string|null
     */
    protected $connection = 'mysql';

    /**
     * The table associated with the model.
     *
     * @var string
     */
    protected $table = 't_agent_order';

    /**
     * The primary key associated with the table.
     *
     * @var string
     */
    protected $pk = 'id';

    const STATUS_WAIT_SETTLEMENT = 1;     //待结算
    const STATUS_REFUND = 2;     //已退款
    const STATUS_SETTLEMENTED = 3;    //已结算

    const STATUS_TEXT_MAP = [
        self::STATUS_WAIT_SETTLEMENT => "待结算",
        self::STATUS_REFUND => "已退款",
        self::STATUS_SETTLEMENTED => "已结算",
    ];
    protected $append = [
        'status_text',
    ];

    public function getStatusTextAttr($value, $data)
    {
        return isset($data['status']) ? self::STATUS_TEXT_MAP[$data['status']] ?? "" : "";
    }

    public function buyuser()
    {
        return $this->hasOne(User::class, 'id', 'buy_user_id');
    }

}
